package com.spa.application.service.admin;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.spa.application.command.admin.AdminUserAddCommand;
import com.spa.application.command.admin.AdminUserDelCommand;
import com.spa.application.command.admin.AdminUserEditCommand;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.SysUserDto;
import com.spa.domain.service.SysAdminUserService;
import com.spa.domain.service.SysRoleService;
import com.spa.infrastructure.mapper.SysUserMapperStruct;
import com.spa.infrastructure.repository.condition.SysUserCondition;
import com.spa.infrastructure.repository.po.SysUserPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Slf4j
public class AdminSysUserApplicationService {

    @Resource
    private SysAdminUserService sysUserService;

//    @Resource
//    private PermissionsService permissionsService;

    @Transactional
    public void addUser(AdminUserAddCommand command) {
        SysUserPO sysUserPO = sysUserService.addUser(command);
//        permissionsService.addRoleForUser(sysUserPO.getUserId().toString(), command.getRoleId());
    }

    public boolean delUser(AdminUserDelCommand command) {
        Assert.isFalse(command.getUserId() == 1,"管理员不能删除");
        return sysUserService.delUser(command);
    }

    public void editUser(AdminUserEditCommand command) {
        SysUserPO sysUserPO = sysUserService.editUser(command);
//        permissionsService.addRoleForUser(String.valueOf(sysUserPO.getUserId()), String.valueOf(command.getRoleId()));
    }

    public PageResult<SysUserDto> pageUser(SysUserCondition command) {
        PageResult<SysUserDto> sysUserDtoPageResult = SysUserMapperStruct.INSTANCE.entity2Dto4Page(sysUserService.pageUser(command));
        return sysUserDtoPageResult;
    }

    public SysUserDto detailsById(Long userId) {
        SysUserDto sysUserDto = SysUserMapperStruct.INSTANCE.entity2Dto(sysUserService.infoById(userId));
        return sysUserDto;
    }

}
